package com.funtl.screw.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funtl.screw.admin.entity.SysPublicParam;
import com.funtl.screw.admin.service.SysPublicParamService;
import com.funtl.screw.common.core.util.QueryRequest;
import com.funtl.screw.common.core.util.ResponseResult;
import com.funtl.screw.common.log.annotation.SysLog;
import com.funtl.screw.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公共参数
 *
 * @author Lucky
 * @since 2019-04-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/param")
@Api(value = "param", tags = "公共参数配置")
public class SysPublicParamController {

    private final SysPublicParamService sysPublicParamService;

    /**
     * 通过key查询公共参数值
     */
    @Inner(value = false)
    @ApiOperation(value = "查询公共参数值", notes = "根据key查询公共参数值")
    @GetMapping("/publicValue/{publicKey}")
    public ResponseResult<String> publicKey(@PathVariable("publicKey") String publicKey) {
        return ResponseResult.ok(sysPublicParamService.getSysPublicParamKeyToValue(publicKey));
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public ResponseResult<Page<SysPublicParam>> getSysPublicParamPage(QueryRequest queryRequest, SysPublicParam sysPublicParam) {
        Page<SysPublicParam> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize());
        return ResponseResult.ok(sysPublicParamService.page(page, Wrappers.query(sysPublicParam)));
    }

    /**
     * 通过id查询公共参数
     *
     * @param publicId id
     * @return R
     */
    @ApiOperation(value = "通过id查询公共参数", notes = "通过id查询公共参数")
    @GetMapping("/{publicId}")
    public ResponseResult<SysPublicParam> getById(@PathVariable("publicId") Long publicId) {
        return ResponseResult.ok(sysPublicParamService.getById(publicId));
    }

    /**
     * 新增公共参数
     *
     * @param sysPublicParam 公共参数
     * @return R
     */
    @ApiOperation(value = "新增公共参数", notes = "新增公共参数")
    @SysLog("新增公共参数")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_syspublicparam_add')")
    public ResponseResult<Boolean> save(@RequestBody SysPublicParam sysPublicParam) {
        SysPublicParam publicParam = sysPublicParamService.getOne(Wrappers.<SysPublicParam>lambdaQuery().eq(SysPublicParam::getPublicKey, sysPublicParam.getPublicKey()));
        if (ObjectUtil.isNotNull(publicParam)) {
            return ResponseResult.failed("该参数已存在");
        }
        return ResponseResult.ok(sysPublicParamService.save(sysPublicParam));
    }

    /**
     * 修改公共参数
     *
     * @param sysPublicParam 公共参数
     * @return R
     */
    @ApiOperation(value = "修改公共参数", notes = "修改公共参数")
    @SysLog("修改公共参数")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_syspublicparam_edit')")
    public ResponseResult<Boolean> updateById(@RequestBody SysPublicParam sysPublicParam) {
        return sysPublicParamService.updateParam(sysPublicParam);
    }

    @ApiOperation(value = "修改公共参数", notes = "修改公共参数")
    @SysLog("修改公共参数")
    @PutMapping("/updateByKey")
    @PreAuthorize("@pms.hasPermission('admin_syspublicparam_edit')")
    public ResponseResult<Boolean> updateByKey(@RequestBody SysPublicParam sysPublicParam) {
        long count = sysPublicParamService.count(Wrappers.<SysPublicParam>lambdaQuery().eq(StrUtil.isNotBlank(sysPublicParam.getPublicKey()), SysPublicParam::getPublicKey, sysPublicParam.getPublicKey()));
        SysPublicParam publicParam = sysPublicParamService.getById(sysPublicParam.getPublicId());
        if (StrUtil.equals(publicParam.getPublicKey(), sysPublicParam.getPublicKey())) {
            return sysPublicParamService.updateParam(sysPublicParam);
        }
        if (count > 0) {
            return ResponseResult.failed("公共参数已存在");
        }
        return sysPublicParamService.updateParam(sysPublicParam);
    }

    /**
     * 通过id删除公共参数
     *
     * @param publicId id
     * @return R
     */
    @ApiOperation(value = "删除公共参数", notes = "删除公共参数")
    @SysLog("删除公共参数")
    @DeleteMapping("/{publicId}")
    @PreAuthorize("@pms.hasPermission('admin_syspublicparam_del')")
    public ResponseResult<Boolean> removeById(@PathVariable Long publicId) {
        return sysPublicParamService.removeParam(publicId);
    }

    /**
     * 同步参数
     *
     * @return R
     */
    @SysLog("同步参数")
    @PutMapping("/sync")
    public ResponseResult<Boolean> sync() {
        return sysPublicParamService.syncParamCache();
    }

}
